﻿<UserControl x:Class="Chapters.Chapter08.StaggeredPanelTester"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:Chapter08="clr-namespace:FSharp.Chapter8;assembly=FSharpLibrary"
			 xmlns:Resources="clr-namespace:Chapters.Resources;assembly=Resources">
	<UserControl.Resources>
		<Resources:StringCollection x:Key="DataSource" />
		<LinearGradientBrush x:Key="BackgroundBrush"
							 StartPoint="0,0"
							 EndPoint="1,0">
			<GradientStop Offset="0"
						  Color="#777777" />
			<GradientStop Offset="1"
						  Color="#222222" />
		</LinearGradientBrush>

		<LinearGradientBrush x:Key="HilightBrush"
							 StartPoint="0,0"
							 EndPoint="1,0">
			<GradientStop Offset="0"
						  Color="#999999" />
			<GradientStop Offset="1"
						  Color="#555555" />
		</LinearGradientBrush>

		<LinearGradientBrush x:Key="SelectedBrush"
							 StartPoint="0,0"
							 EndPoint="1,0">
			<GradientStop Offset="0"
						  Color="#c1dbe8" />
			<GradientStop Offset="0.5"
						  Color="#008fc7" />
			<GradientStop Offset="1"
						  Color="#066caa" />
		</LinearGradientBrush>

		<DataTemplate x:Key="StringTemplate">
			<TextBlock x:Name="Text"
					   Text="{Binding}"
					   FontSize="24"
					   Foreground="Wheat"
					   HorizontalAlignment="Left"
					   VerticalAlignment="Center"
					   Margin="10" />

			<DataTemplate.Triggers>
				<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
							 Value="True">
					<Setter Property="Foreground"
							Value="#333333"
							TargetName="Text" />
				</DataTrigger>
			</DataTemplate.Triggers>
		</DataTemplate>

		<Style TargetType="ListBoxItem">
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="ListBoxItem">
						<Border x:Name="Border"
								CornerRadius="20,0,0,20"
								Background="{StaticResource BackgroundBrush}"
								BorderBrush="Black"
								BorderThickness="1"
								SnapsToDevicePixels="True">
							<ContentPresenter Content="{TemplateBinding Content}"
											  ContentTemplate="{TemplateBinding ContentTemplate}" />
						</Border>

						<ControlTemplate.Triggers>
							<Trigger Property="IsMouseOver"
									 Value="True">
								<Setter Property="Background"
										Value="{StaticResource HilightBrush}"
										TargetName="Border" />
							</Trigger>
							<Trigger Property="IsSelected"
									 Value="True">
								<Setter Property="Background"
										Value="{StaticResource SelectedBrush}"
										TargetName="Border" />
								<Setter Property="BorderBrush"
										Value="#0075b0"
										TargetName="Border" />
							</Trigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>

	</UserControl.Resources>

	<DockPanel>
		<CheckBox x:Name="_deferredScroll"
				  DockPanel.Dock="Top"
				  Margin="10">Deferred Scrolling</CheckBox>
		<Grid>
			<ListBox x:Name="_itemsControl"
					 ItemsSource="{StaticResource DataSource}"
					 ItemTemplate="{StaticResource StringTemplate}"
					 HorizontalContentAlignment="Stretch"
					 VerticalContentAlignment="Stretch"
					 Margin="10">

				<ListBox.Template>
					<ControlTemplate>
						<ScrollViewer CanContentScroll="True"
									  HorizontalScrollBarVisibility="Visible"
									  IsDeferredScrollingEnabled="{Binding IsChecked, ElementName=_deferredScroll}">
							<ItemsPresenter />
						</ScrollViewer>
					</ControlTemplate>
				</ListBox.Template>

				<ListBox.ItemsPanel>
					<ItemsPanelTemplate>
						<Chapter08:StaggeredPanel ItemWidth="200"
												  ItemHeight="150"
												  StaggerValue="0.5"
												  IsItemsHost="True" />
					</ItemsPanelTemplate>
				</ListBox.ItemsPanel>

			</ListBox>
		</Grid>

		<ListBox VirtualizingStackPanel.IsVirtualizing="True"
				 VirtualizingStackPanel.VirtualizationMode="Recycling"
				 ItemsSource="{StaticResource DataSource}"
				 ItemTemplate="{StaticResource StringTemplate}" />
	</DockPanel>
</UserControl>
